﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace web.httpfetch
{
    public struct PageState
    {
        public int offset;
        public bool isGetPageEncoding;
        public int length;
    }

    class Program
    {
        static void Main(string[] args)
        {
            // used to build entire input
            String path = "D:\\workspace\\FileName.txt";
            StringBuilder sb = new StringBuilder();
            List<string> urlString = new List<string>();
            List<StringBytePair> bytePairs = new List<StringBytePair>();
            List<string> urlList = new List<string>();
            ParserState state = ParserState.Normal;
            string mainCharset = "UTF-8";
           // urlString.Add("http://sourceforge.net/");
           // urlString.Add("http://www.nytimes.com");
            urlString.Add("http://www.bbc.com");
            urlString.Add("http://www.google.com.au/");
            urlString.Add("http://www.dict.org/bin/Dict?Form=Dict2&Database=*&Query=develop");
            urlString.Add("http://news.163.com/");
            urlString.Add("http://dict.cn/mauling");
            urlString.Add("http://www.dict.org/bin/Dict?Form=Dict2&Database=*&Query=develop");
            string realURL = urlString[0];
            PageState pageState = new PageState();
            pageState.offset = 0;

            string temp = HtmlParserHelper.getHtmlFile(realURL, mainCharset);
            int i = 0;
            TextWriter tw = new StreamWriter(path, false, Encoding.GetEncoding(mainCharset));

            while(i<temp.Length)
            {
                int length=i+1;
                switch (temp[i])
                {
                    case '<':
                        Tags tag = new Tags();
                        while (temp[length] != ' ' && temp[length] != '>') length++;
                        tag.TagsName = temp.Substring(i + 1, length - i - 1);

                        //switch (tag.Type)
                        //{
                        //    case TagsType.DoubleCloseTags:
                        //        getTagContent(temp.Substring(i + 1, length - i - 1), length + 1);
                        //        break;
                        //}
                        switch (tag.TagsName)
                        {
                            case "a":
                            case "A":
                                urlList.Add(HtmlParserHelper.getProperty(temp, length, "href"));
                                goto case "p";
                            case "img":
                                //hrefPropery = getProperty(temp, length, "src");
                                //break;
                            case "b":
                            case "B":
                            case "H1":
                            case "h1":
                            case "p":
                            case "P":
                            case "pre":
                            case "/a":
                            case "/b":
                            case "/H1":
                            case "H2":
                            case "/H2":
                            case "h2":
                            case "/h2":
                            case "H3":
                            case "/H3":
                            case "h3":
                            case "/h3":
                            case "/pre":
                            case "div":
                            case "/div":
                            case "td":
                            case "/td":
                            case "tr":
                            case "/tr":
                            case "span":
                            case "/span":
                            case "strong":
                            case "/strong":
                            case "li":
                            case "/li":

                                length = HtmlParserHelper.moveToThisTagEnd(temp, length);
                                //Console.Write(temp.Substring(length+1,moveToNextTagStart(temp,length)-length-1)+" ");
                                string content = temp.Substring(length + 1, HtmlParserHelper.moveToNextTagStart(temp, length) - length - 1) + " ";
                                Regex r = new Regex("^[ \t\r\n]*$");
                                if (r.IsMatch(content) == false)
                                    tw.WriteLine(content);
                                break;

                            case "meta":
                                string charset = HtmlParserHelper.getProperty(temp, length, "charset");

                                if (charset != null && charset != "UTF-8")
                                {
                                    temp = HtmlParserHelper.getHtmlFile(realURL, charset);
                                    mainCharset = charset;
                                    tw = new StreamWriter(@"C:\workspace\FileName.txt", false, Encoding.GetEncoding(mainCharset));
                                }
                                //Console.Write(temp);
                                break;
                            case "script":

                                break;
                        }
                        i=length;
                        break;
                }
                i++;
            }
            //TextWriter tw = new StreamWriter(@"C:\workspace\FileName.txt", false, Encoding.GetEncoding(mainCharset));
            //tw.Write(temp);
            

            foreach (string s in urlList)
            {
                tw.WriteLine(s);
            }
            tw.Close();

            Process notePad = new Process();

            notePad.StartInfo.FileName = "notepad.exe";
            notePad.StartInfo.Arguments = path;

            notePad.Start();

            //System.Diagnostics.Process proc = new System.Diagnostics.Process();
            //proc.EnableRaisingEvents = false;
            //proc.StartInfo.FileName = "c:\\workspace\\firebird\\1.bat";
            //proc.Start();
            // print out page source
            //Console.WriteLine(sb.ToString());
        }
    }
}
